package com.letter.otools.day.diary;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.letter.otools.day.R;
import com.letter.otools.day.listener.PositionListener;
import com.letter.otools.day.util.ImageUtils;
import com.letter.otools.day.util.PopupUtils;
import com.letter.otools.day.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class DiaryTimelineAdapter extends RecyclerView.Adapter<DiaryTimelineAdapter.DiaryViewHolder> {

    private List<Diary> mDiaryList;
    int showNum = 1;

    private PositionListener positionListener;

    public void setPositionListener(PositionListener positionListener) {
        this.positionListener = positionListener;
    }

    public DiaryTimelineAdapter(List<Diary> diaryList) {
        mDiaryList = diaryList;
    }

    public DiaryTimelineAdapter(List<Diary> mDiaryList, int showNum) {
        this.mDiaryList = mDiaryList;
        this.showNum = showNum;
    }

    @NonNull
    @Override
    public DiaryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_diary_timeline, parent, false);
        return new DiaryViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull DiaryViewHolder holder, int position) {

        Diary diary = mDiaryList.get(position);
        if (position == 0 && showNum == 1) {
            holder.diaryNumLay.setVisibility(View.VISIBLE);
            holder.diaryNum.setText(String.valueOf(mDiaryList.size()));
        } else {
            holder.diaryNumLay.setVisibility(View.GONE);
        }

        if (showNum == 0) {
            holder.tv_date1.setVisibility(View.VISIBLE);
            holder.tv_date1.setText(TimeUtils.millis2String(diary.getCreateTime(), "yyyy-MM-dd"));
        }

        // 设置日记数据
        holder.tvDate.setText(TimeUtils.millis2String(diary.getCreateTime(), "HH:mm"));
        holder.tvTitle.setText(diary.getTitle());
        holder.tvTitle.setVisibility(StringUtils.isEmpty(diary.getTitle()) ? View.GONE : View.VISIBLE);
        holder.tvContent.setText(diary.getContent());

        // 隐藏最后一项的时间线
        if (position == mDiaryList.size() - 1) {//diary.isLast()
            holder.timelineLine.setVisibility(View.INVISIBLE);
        } else {
            holder.timelineLine.setVisibility(View.VISIBLE);
        }

        String loadInvoicePic = diary.getImages();
        if (StringUtils.isNotEmpty(loadInvoicePic)) {
            List<ImageView> imageViewList = new ArrayList();
            imageViewList.add(holder.image1);
            imageViewList.add(holder.image2);
            imageViewList.add(holder.image3);
            imageViewList.add(holder.image4);
            List<View> imageViewList2 = new ArrayList();
            imageViewList2.add(holder.imageView1);
            imageViewList2.add(holder.imageView2);
            imageViewList2.add(holder.imageView3);
            imageViewList2.add(holder.imageView4);
            holder.imageLinView.setVisibility(View.VISIBLE);
            List<String> imageList = Arrays.asList(loadInvoicePic.split(","));
            for (int i = 0; i < 4; i++) {
                final int fi = i;
                if (i < imageList.size() && StringUtils.isNotEmpty(imageList.get(i))) {
                    imageViewList.get(i).setVisibility(View.VISIBLE);
                    imageViewList2.get(i).setVisibility(View.VISIBLE);
                    String imageUrl = imageList.get(i);
                    ImageUtils.showImage(imageViewList.get(i).getContext(), imageUrl, imageViewList.get(i));
                    imageViewList.get(i).setOnClickListener(v -> {
                        PopupUtils.preViewImages(imageViewList.get(fi).getContext(), imageViewList.get(fi), fi, imageList);
                    });
                } else {
                    imageViewList.get(i).setVisibility(View.INVISIBLE);
                    imageViewList2.get(i).setVisibility(View.INVISIBLE);
                }
            }
        } else {
            holder.imageLinView.setVisibility(View.GONE);
        }

        // 添加标签
//        holder.tagsContainer.removeAllViews();
//        for (String tag : diary.getTags()) {
//            TextView tagView = createTagView(holder.itemView, tag);
//            holder.tagsContainer.addView(tagView);
//        }

        // 点击事件
        holder.itemView.setOnClickListener(v -> {
            // 打开日记详情
            positionListener.onPosition(1, diary);
        });
    }

    @Override
    public int getItemCount() {
        return mDiaryList.size();
    }

    // 创建标签视图
    private TextView createTagView(View itemView, String tagText) {
        TextView tagView = new TextView(itemView.getContext());
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
        );
        params.setMarginEnd(SizeUtils.dp2px(8));
        tagView.setLayoutParams(params);
        tagView.setText(tagText);
        tagView.setTextSize(12);
        tagView.setTextColor(itemView.getResources().getColor(R.color.colorTagText));
        tagView.setBackgroundResource(R.drawable.tag_bg);
        tagView.setPadding(8, 2, 8, 2);
        return tagView;
    }

    public static class DiaryViewHolder extends RecyclerView.ViewHolder {
        TextView tvDate, tv_date1;
        TextView tvTitle;
        TextView tvContent;
        ImageView timelineDot;
        View timelineLine;
        LinearLayout tagsContainer;

        View diaryItemLay, diaryNumLay;
        TextView diaryNum;

        public View imageLinView;
        public ImageView image1, image2, image3, image4;
        public View imageView1, imageView2, imageView3, imageView4;

        public DiaryViewHolder(View itemView) {
            super(itemView);
            tv_date1 = itemView.findViewById(R.id.tv_date1);
            tvDate = itemView.findViewById(R.id.tv_date);
            tvTitle = itemView.findViewById(R.id.tv_title);
            tvContent = itemView.findViewById(R.id.tv_content);
            timelineDot = itemView.findViewById(R.id.timeline_dot);
            timelineLine = itemView.findViewById(R.id.timeline_line);
            tagsContainer = itemView.findViewById(R.id.tags_container);
            diaryItemLay = itemView.findViewById(R.id.diary_item_lay);
            diaryNum = itemView.findViewById(R.id.diary_num);
            diaryNumLay = itemView.findViewById(R.id.diary_num_lay);
            image1 = itemView.findViewById(R.id.image1);
            imageView1 = itemView.findViewById(R.id.imageView1);
            image2 = itemView.findViewById(R.id.image2);
            imageView2 = itemView.findViewById(R.id.imageView2);
            image3 = itemView.findViewById(R.id.image3);
            imageView3 = itemView.findViewById(R.id.imageView3);
            image4 = itemView.findViewById(R.id.image4);
            imageView4 = itemView.findViewById(R.id.imageView4);
            imageLinView = itemView.findViewById(R.id.imageLinView);
        }
    }
}

